package com.kellycheung.chicken_go_go;

import android.graphics.Canvas;
import android.graphics.Paint;

public class ScrollableObj extends GameObject {
	public enum ScrollDirection {
		none(0), vertical(10), horizontal(20);
		
		@SuppressWarnings("unused")
		private final int iVal;
		
		private ScrollDirection( int p_iVal ) {
			this.iVal = p_iVal;
		}
	}
	
	protected final static float F_DEF_MAX_X_SPEED = 20f;
	protected final static float F_DEF_X_ACC_INC = -1f;
	protected final static float F_DEF_X_ACC_DEC = 0.5f;
	
	protected ScrollDirection sdDir;
	
	protected int iBGRepeatCnt;
	protected boolean bMove;

	public ScrollableObj( GameScene p_gsScene , int p_iZOrder ) {
		this( p_gsScene , p_iZOrder , GOAnchor.UpLeft , 0f , 0f );
		// TODO Auto-generated constructor stub
	}

	public ScrollableObj( GameScene p_gsScene , int p_iZOrder , GOAnchor p_goa , float p_fX , float p_fY  ) {
		super( p_gsScene , p_iZOrder , p_goa , p_fX , p_fY );
		// TODO Auto-generated constructor stub
		
		this.bMove = false;
	}
	
	public void destroy() {
		try {
			this.sdDir = null;
			
			super.destroy();
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "ScrollableObj.destroy" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void setDirection( ScrollDirection p_sd ) {
		try {
			this.sdDir = p_sd;
			switch ( this.sdDir ) {
			case vertical:
				this.iBGRepeatCnt = (int)Math.floor( this.gsScene.geMain.gcScreenSize.fX / this.getWidth() ) + 1;
				break;
			case horizontal:
				this.iBGRepeatCnt = (int)Math.floor( this.gsScene.geMain.gcScreenSize.fY / this.getHeight() ) + 1;
				break;
			default:
				break;
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "ScrollableObj.setDirection" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void scroll( float p_fDisp ) {
		try {
			switch ( this.sdDir ) {
			case vertical:
				this.scrollY( p_fDisp );
				break;
			case horizontal:
				this.scrollX( p_fDisp );
				break;
			default:
				break;
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "ScrollableObj.scroll" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void scrollX( float p_fDisp ) {
		try {
			this.gcCoord.fX += p_fDisp;
			
			while ( this.gcCoord.fX > this.gsScene.geMain.gcScreenSize.fX ) {
				this.gcCoord.fX -= this.getWidth();
			}
			while ( this.gcCoord.fX < 0 ) {
				this.gcCoord.fX += this.getWidth();
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "ScrollableObj.scrollX" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void scrollY( float p_fDisp ) {
		try {
			this.gcCoord.fY += p_fDisp;
			
			while ( this.gcCoord.fY > this.gsScene.geMain.gcScreenSize.fY ) {
				this.gcCoord.fY -= this.getHeight();
			}
			while ( this.gcCoord.fY < 0 ) {
				this.gcCoord.fY += this.getHeight();
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "ScrollableObj.scrollY" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}

	@Override
	public void draw( Canvas p_c , Paint p_p ) {
		try {
			GCoordinate gcOrig = new GCoordinate( this.gcCoord );
			GCoordinate gcPosOffset = new GCoordinate( 0f , 0f );
			GCoordinate gcNegOffset = new GCoordinate( 0f , 0f );
			
			switch ( this.sdDir ) {
			case vertical:
				gcPosOffset.set( 0f , this.getHeight() );
				gcNegOffset.set( 0f , -this.getHeight() );
				break;
			case horizontal:
				gcPosOffset.set( this.getWidth() , 0f );
				gcNegOffset.set( -this.getWidth() , 0f );
				break;
			default:
				break;
			}
			
			for ( int i = 0 ; i < this.iBGRepeatCnt ; i++ ) { 
				this.setCoord( gcOrig.offset( gcPosOffset ) );
				super.draw( p_c , p_p );
			}
			this.gcCoord.set( gcOrig );
			
			for ( int i = 0 ; i < this.iBGRepeatCnt ; i++ ) { 
				this.setCoord( gcOrig.offset( gcNegOffset ) );
				super.draw( p_c , p_p );
			}
			this.setCoord( gcOrig );
			super.draw( p_c , p_p );
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "ScrollableObj.draw" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
}
